//
//  SetViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "SetViewController.h"
#import "AboutViewController.h"
#import "SetTableViewCell.h"
#import "UserLogoutApi.h"
#import "UserDeleteApi.h"
#import "BlackUserListViewController.h"

@interface SetViewController ()

@property (nonatomic, strong) UIButton *loginoutBtn;

@property (nonatomic, strong) NSArray *cellImages;
@property (nonatomic, strong) NSArray *cellTitles;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.centerTitle.text = @"设置";
    
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = 50;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetTableViewCell" bundle:nil] forCellReuseIdentifier:@"SetTableViewCell"];
    self.tableView.frame = CGRectMake(0, UIDevice.vg_navigationFullHeight, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - UIDevice.vg_navigationFullHeight);
    self.tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, 50)];
        view;
    });
    /*
     未登录
     
     设置：黑名单 关于我们
     
     已登录
     
     设置：黑名单 关于我们 删除账号。。退出登录
     
     */
    
    self.cellImages = @[@"black_people",@"about",@""];
    self.cellTitles = @[@"黑名单",@"关于我们",@"删除账号"];
    
    UIButton *argeeBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(SL_WINDOW_WIDTH/2 - 100, self.tableView.height - UIDevice.vg_safeDistanceBottom - 40, 100, 40)];
    argeeBtn1.tag = 1;
    argeeBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
    [argeeBtn1 setTitle:@"《隐私协议》" forState:UIControlStateNormal];
    if (@available(iOS 13.0, *)) {
        [argeeBtn1 setTitleColor:[UIColor linkColor] forState:UIControlStateNormal];
    } else {
        [argeeBtn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    [argeeBtn1 addTarget:self action:@selector(argeeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:argeeBtn1];
    
    UIButton *argeeBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(SL_WINDOW_WIDTH/2, self.tableView.height - UIDevice.vg_safeDistanceBottom - 40, 100, 40)];
    argeeBtn2.tag = 2;
    argeeBtn2.titleLabel.font = [UIFont systemFontOfSize:12];
    [argeeBtn2 setTitle:@"《用户协议》" forState:UIControlStateNormal];
    if (@available(iOS 13.0, *)) {
        [argeeBtn2 setTitleColor:[UIColor linkColor] forState:UIControlStateNormal];
    } else {
        [argeeBtn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    [argeeBtn2 addTarget:self action:@selector(argeeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:argeeBtn2];
    
    UIButton *loginoutBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, self.tableView.height - UIDevice.vg_safeDistanceBottom - 80, SL_WINDOW_WIDTH - 80, 40)];
    loginoutBtn.backgroundColor = kThemeColor;
    loginoutBtn.layer.cornerRadius = loginoutBtn.height/2;
    loginoutBtn.layer.masksToBounds = YES;
    [loginoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [loginoutBtn setTitleColor:kColor333333 forState:UIControlStateNormal];
    [loginoutBtn addTarget:self action:@selector(loginoutClick) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:loginoutBtn];
    self.loginoutBtn = loginoutBtn;
    
    [self refreshAfterLogin];
}

# pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([LocalData fetchUserInfo]) {
        return 3;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetTableViewCell" forIndexPath:indexPath];
    cell.imageStr = self.cellImages[indexPath.row];
    cell.title = self.cellTitles[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {//黑名单
        if (![LocalData fetchUserInfo]) {
            [self loginAction];
            return;
        }
        BlackUserListViewController *vc = [[BlackUserListViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 1) {//关于我们
        AboutViewController *vc = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 2) {//删除账号
        [self deleteAccountClick];
    }
    
}

- (void)loginAction
{
    [[AppLogic sharedInstance] presentLoginVC];
}
- (void)refreshAfterLogin
{
    if ([LocalData fetchUserInfo]) {
        self.loginoutBtn.hidden = NO;
    } else {
        self.loginoutBtn.hidden = YES;
    }
    [self.tableView reloadData];
}

- (void)loginoutClick
{
    VHWeakSelf
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否要退出登录" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        VHStrongSelf
        UserLogoutApi *api = [[UserLogoutApi alloc] initWithToken:[LocalData getToken]];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            if (api.success) {
                [LocalData clearLoginInfo];
                [self refreshAfterLogin];
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:defaultAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)argeeBtnClick:(UIButton *)button
{
    if (button.tag == 1) {
        //隐私协议
        [[AppLogic sharedInstance] pushToWebViewVC:@"隐私协议" url:@"https://www.7pa.com/yhhnqply.html"];
    } else {
        //用户协议
        [[AppLogic sharedInstance] pushToWebViewVC:@"用户协议" url:@"https://www.7pa.com/yhhnqply.html"];
    }
}

- (void)deleteAccountClick
{
    VHWeakSelf
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号删除后系统会清除个人信息无法登录，且不可找回。确定要删除账号吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        VHStrongSelf
        UserDeleteApi *api = [[UserDeleteApi alloc] initWithUserId:[LocalData fetchUserInfo].userId];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            if (api.success) {
                [LocalData clearLoginInfo];
                [self refreshAfterLogin];
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:defaultAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

@end
